package com.fanghuyun.iswaf.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import com.fanghuyun.iswaf.util.StringUtil;

public class SysConfig {
	
	public Properties getSysConfigProperties(){
		Properties p = new Properties();
		try {
			p.load(new FileInputStream(new File(getIsWafConfDirectory()+"SysConfig.properties")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}
	
	public String getServerPath(){
		try {
			return new File(new File("").getAbsolutePath()).getParent().replaceAll("\\\\", "/")+"/";
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * IsWaf Directory
	 * @return
	 */
	public String getIsWafDirectory(){
		File f = new File(getServerPath()+"iswaf/");
		if(!f.exists()){
			f.mkdir();
		}
		return getServerPath()+"iswaf/";
	}
	
	public String getIsWafConfDirectory(){
		File f = new File(getIsWafDirectory()+"conf/");
		if(!f.exists()){
			f.mkdir();
		}
		return getIsWafDirectory()+"conf/";
	}
	
	public String getDatabaseDirectory(){
		File f = new File(getIsWafDirectory()+"database/");
		if(!f.exists()){
			f.mkdir();
		}
		return getIsWafDirectory()+"database/";
	}
	
	public static String getRequestFileRealPath(HttpServletRequest request){
		String webRoot = request.getSession().getServletContext().getRealPath("").replaceAll("\\\\", "/"),
			   context = request.getContextPath(), 
			   uri = request.getRequestURI();
		if(StringUtil.isNotEmpty(context)&&webRoot.endsWith(context)){
			return webRoot.substring(0,webRoot.indexOf(context))+uri;
		}
		return webRoot+uri;
	}
	
	public String getRequestParameterAndCookie(HttpServletRequest request){
		Enumeration<?> e = request.getParameterNames();
		StringBuilder sb = new StringBuilder("Request:");
		List<Object> ls = new ArrayList<Object>();
		while (e.hasMoreElements()) {
			String key = (String)e.nextElement();
			ls.add(key+"="+request.getParameter(key));
		}
		sb.append(StringUtils.join(ls.toArray(),"&")).append("\tCookie:");
		ls.clear();
		Cookie[] c = request.getCookies();
		if(StringUtil.isNotEmpty(c)){
			for(Cookie cookie:c){
				ls.add(cookie.getName()+"="+cookie.getValue());
			}
		}
		return sb.append(StringUtils.join(ls.toArray(),"&")).toString();
	}
	
	public Map<String,Object> getRequestParameter(HttpServletRequest request){
		Enumeration<?> e = request.getParameterNames();
		Map<String,Object> requestParameters = new HashMap<String, Object>();
		while (e.hasMoreElements()) {
			String key = (String)e.nextElement();
			requestParameters.put(key, request.getParameter(key));
		}
		return requestParameters;
	}
	
	public String getRequestHeaders(HttpServletRequest request){
		Enumeration<?> e = request.getHeaderNames();
		List<String> ls = new ArrayList<String>();
		while (e.hasMoreElements()) {
		    String name = (String) e.nextElement();
		    ls.add(name+"="+request.getHeader(name));
		}
		return StringUtils.join(ls.toArray(), "&");
	}
	
}
